package com.bc.bcapi.service;

import com.bc.bcapi.dao.T_ProjectMapper;
import com.bc.bcmodel.resp.ProjectDetailResp;
import com.bc.commoncore.util.HttpUtil;
import com.bc.commoncore.util.JsonUtil;
import com.bc.commonentity.common.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.math.BigInteger;
import java.util.Set;

import com.bc.bcapi.dao.T_Usdt_exchange_priceMapper;
import com.bc.bcmodel.domain.T_Usdt_exchange_priceModel;

/**
 * @Name T_Usdt_exchange_priceService * @Description  * @Author generation * @Date 2018/9/11 15:33:09 * @Version 1.0
 **/
@Service
public class T_Usdt_exchange_priceService {
    @Resource
    T_Usdt_exchange_priceMapper t_Usdt_exchange_priceMapper;
    @Resource
    T_ProjectMapper t_projectMapper;

    static Set<String> SYMBOL_SET = new HashSet<String>() {
        {
            add("ETH");
            add("EOS");
        }
    };

    /**
     * 保存单条数据     *      * @return
     */
    public int insert(T_Usdt_exchange_priceModel req) {
        return t_Usdt_exchange_priceMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */
    public int insertBatch(List<T_Usdt_exchange_priceModel> req) {
        return t_Usdt_exchange_priceMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    public T_Usdt_exchange_priceModel findByNamedParam(T_Usdt_exchange_priceModel req) {
        return t_Usdt_exchange_priceMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */
    public List<T_Usdt_exchange_priceModel> getAllList(T_Usdt_exchange_priceModel req) {
        return t_Usdt_exchange_priceMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    public PageInfo<T_Usdt_exchange_priceModel> getList(T_Usdt_exchange_priceModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Usdt_exchange_priceModel> list = t_Usdt_exchange_priceMapper.findByNamedParamList(req);
        return new PageInfo<T_Usdt_exchange_priceModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */
    public int update(T_Usdt_exchange_priceModel req) {
        return t_Usdt_exchange_priceMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */
    public int delete(BigInteger id) {
        return t_Usdt_exchange_priceMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    public long count(T_Usdt_exchange_priceModel req) {
        return t_Usdt_exchange_priceMapper.count(req);
    }

    /**
     * 去币安同步usdt的价格
     */
    public JsonResult setUsdtExchangePrice() {
        JsonResult js = new JsonResult();
        String dataStr = cn.hutool.http.HttpUtil.get("https://api.binance.com/api/v3/ticker/price");
//        String dataStr = HttpUtil.sendGet("https://api.binance.com/api/v3/ticker/price", "");
        List<T_Usdt_exchange_priceModel> list = JsonUtil.str2list(dataStr, T_Usdt_exchange_priceModel.class);
        for (T_Usdt_exchange_priceModel item :
                list) {
            String orgsymbol = item.getSymbol();
            if (!orgsymbol.endsWith("USDT")) {
                continue;
            }
            String symbol = orgsymbol.replace("USDT", "");
            String to = "USDT";
            T_Usdt_exchange_priceModel t_usdt_exchange_priceModel = new T_Usdt_exchange_priceModel();
            t_usdt_exchange_priceModel.setSymbol(symbol);
            t_usdt_exchange_priceModel.setTo_symbol(to);
            t_usdt_exchange_priceModel.setPrice(item.getPrice());
            /**
             * 查询是否存在
             * */
            T_Usdt_exchange_priceModel countModel = new T_Usdt_exchange_priceModel();
            countModel.setSymbol(symbol);
            int counter = t_Usdt_exchange_priceMapper.count(countModel);
            if (counter > 0) {
                t_Usdt_exchange_priceMapper.update(t_usdt_exchange_priceModel);
            } else {
                t_Usdt_exchange_priceMapper.insert(t_usdt_exchange_priceModel);
            }
        }
        return js;
    }

    /**
     * check symbol is support
     *
     * @param symbol
     * @return
     */
    private boolean isSupportSymbol(String symbol) {
        return SYMBOL_SET.contains(symbol);
    }

    /**
     * eth/eos->usdt->coin
     */
    public BigDecimal exchageTokens(BigDecimal fromTokenCount, String contractId, String pid) {
        ProjectDetailResp projectDetailResp = t_projectMapper.getDetailByPid(pid);
        if (null == projectDetailResp) {
            throw new RuntimeException("项目信息不存在");
        }
        if (!isSupportSymbol(contractId)) {
            throw new RuntimeException("交易类型不支持");
        }
        //get ETH/USDT
        T_Usdt_exchange_priceModel fromToken2Usdt_priceModel = t_Usdt_exchange_priceMapper.selectBySymbol(contractId);
        BigDecimal fromToken2Usdt = fromToken2Usdt_priceModel.getPrice();
        //get TOKEN/USDT
        BigDecimal toToken2Usdt_price = projectDetailResp.getToken_price();
        BigDecimal rise = fromToken2Usdt.divide(toToken2Usdt_price, 8, BigDecimal.ROUND_UP);
        return rise.multiply(fromTokenCount);
    }

}